<?php

namespace app\api\controller;

use app\BaseController;
use app\common\model\PatientModel;
use app\common\model\ResearchModel;
use app\common\model\TagModel;
use think\facade\Db;

class  Research extends BaseController
{

    /**
     * @var  ResearchModel
     */
    private $model;

    public function initialize()
    {
        $this->model = new ResearchModel();
    }

    /**
     * 患者列表
     */
    public function index()
    {
        $result = [
            "status" => "no",
            "data" => [],
            "msg" => "查找失败"
        ];
        $option = $this->option();
        $result["status"] = "ok";
        $result["msg"] = "查找成功";

        $data = $this->model->pageData($option, '');
        $result["data"] = $data;

        return json($result);
    }

    /**
     * 医生的已经加入
     */
    public function my()
    {
        $result = [
            "status" => "no",
            "data" => [],
            "msg" => "查找失败"
        ];
        $option = $this->option();
        $result["status"] = "ok";
        $result["msg"] = "查找成功";

        //医生参与的科研项目
        $userId = $option['user']['account_id'] ? $option['user']['account_id'] : 0;
        if (empty($userId)) {
            $result['msg'] = '医生id缺少';
            return $result;
        }
        $projectIds = Db::table("gy_research_doc")
            ->where('status', 1)
            ->where("doc_id", $userId)->column("project_id");
        $where = [];
        $where[] = ['id', "IN", array_unique($projectIds)];

        $data = $this->model->pageData($option, '', $where);
        $result["data"] = $data;

        return json($result);
    }

    /**
     * 医生的可以申请
     */
    public function other()
    {
        $result = [
            "status" => "no",
            "data" => [],
            "msg" => "查找失败"
        ];
        $option = $this->option();
        $result["status"] = "ok";
        $result["msg"] = "查找成功";

        //医生参与的科研项目
        $userId = $option['user']['account_id'] ? $option['user']['account_id'] : 0;
        if (empty($userId)) {
            $result['msg'] = '医生id缺少';
            return $result;
        }
        $projectIds = Db::table("gy_research_doc")
            ->where('status', "IN", [1, 2])
            ->where("doc_id", $userId)->column("project_id");
        $where = [];
        $where[] = ['id', "NOT IN", array_unique($projectIds)];
        $data = $this->model->pageData($option, '', $where);

        //查看状态
        $projectWait = Db::table("gy_research_doc")
            ->where('status', 0)
            ->where("doc_id", $userId)->column("project_id");

        $projects = [];
        foreach ($data['list'] as $key => $v) {
            $temp = $v;
            if (in_array($v['id'], $projectWait)) {
                $temp['doc_status'] = 'wait';
            } else {
                $temp['doc_status'] = 'no';
            }
            $projects[] = $temp;
        }

        $result["data"] = $projects;
        return json($result);
    }

    public function patientKy()
    {
        $result = [
            "status" => "no",
            "data" => [],
            "msg" => "查找失败"
        ];

        $id = $this->request->param("id", 0);
        if (empty($id)) {
            $result['msg'] = 'id参数缺少';
            return $result;
        }

        $option = $this->option();
        $result["status"] = "ok";
        $result["msg"] = "查找成功";

        $projectIds = Db::table("gy_research_patient")
            ->where("patient_id", $id)
            ->column("project_id");
        $where = [];
        $where[] = ['id', "IN", array_unique($projectIds)];
        $data = $this->model->pageData($option, '', $where);
        $projects = [];
        foreach ($data['list'] as $key => $v) {
            $temp = $v;
            $projects[] = $temp;
        }

        $result["data"] = $projects;
        return json($result);
    }

    public function detail()
    {
        $result = [
            "status" => "no",
            "data" => [],
            "msg" => "查找失败"
        ];
        $id = $this->request->param("id", 0);
        if (empty($id)) {
            $result['msg'] = 'id参数缺少';
            return $result;
        }
        $data = Db::table("gy_research_project")->where("id", $id)->find();
        if (empty($data)) {
            $result['msg'] = "数据查找失败";
            return $result;
        }
        //医生的状态
        $option = $this->option();
        $userId = $option['user']['account_id'] ? $option['user']['account_id'] : 0;
        $projectWait = Db::table("gy_research_doc")
            ->where('project_id', $id)
            ->where("doc_id", $userId)
            ->find();
        $data['doc_status'] = empty($projectWait) ? 100 : $projectWait['status'];

        $result['status'] = 'ok';
        $result['msg'] = "查找成功";
        $result['data'] = $data;
        return json($result);
    }

    public function patient()
    {
        $result = [
            "status" => "no",
            "data" => [],
            "msg" => "查找失败"
        ];
        $id = $this->request->param("id", 0);
        if (empty($id)) {
            $result['msg'] = 'id参数缺少';
            return $result;
        }
        $data = Db::table("gy_research_project")->where("id", $id)->find();
        if (empty($data)) {
            $result['msg'] = "数据查找失败";
            return $result;
        }
        $ids = Db::table("gy_research_patient")
            ->where("project_id", $id)
            ->column('patient_id');

        $option = $this->option();
        $where = [];
        $where[] = ['id', "IN", array_unique($ids)];
        $patientModel = new PatientModel();
        $data = $patientModel->pageData($option, '', $where);
        $result['status'] = 'ok';
        $result['msg'] = "查找成功";
        $result['data'] = $data;
        return json($result);
    }

    public function makeDoc()
    {
        $result = [
            "status" => "no",
            "data" => [],
            "msg" => "申请失败"
        ];
        $id = $this->request->param("id", 0);
        if (empty($id)) {
            $result['msg'] = 'id参数缺少';
            return $result;
        }
        $data = Db::table("gy_research_project")->where("id", $id)->find();
        if (empty($data)) {
            $result['msg'] = "数据查找失败";
            return $result;
        }
        $option = $this->option();
        $userId = $option['user']['account_id'] ? $option['user']['account_id'] : 0;
        $projectWait = Db::table("gy_research_doc")
            ->where('project_id', $id)
            ->where("doc_id", $userId)
            ->find();
        if (!empty($projectWait)) {
            $result['msg'] = '医生已经参与项目';
            return $result;
        }

        $insertResult = Db::table("gy_research_doc")->insert([
            "project_id" => $id,
            "doc_id" => $userId,
            "status" => 0,
        ]);
        if ($insertResult) {
            $result['status'] = 'ok';
            $result['msg'] = '申请成功';
        }
        return json($result);
    }

    //检查是否登录
    private function checkLogin()
    {
        $result = [
            "status" => "noLogin",
            "data" => [],
            "msg" => "请登录"
        ];

        if ($this->user == null) {
            return json($result);
        }
    }
}